﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wheeler.Web.Utility;
using Wheeler.Web.ViewModels;
using System.Web.Mvc;

namespace Wheeler.Web.Utility
{
    public class BaseController : System.Web.Mvc.Controller
    {
        private UserViewModel _currentUser = null;
        
        /// <summary>
        /// Default Constructor called by the ASP.NET Runtime Engine
        /// </summary>
        public BaseController()
        {
            WebUtil.ReLoginUser();
        }

        /// <summary>
        /// Used for Depecdency Injection for Automated Testing
        /// </summary>
        /// <param name="currentUser"></param>
        public BaseController(UserViewModel currentUser)
        {
            _currentUser = currentUser;
        }

        /// <summary>
        /// Gets the Currently Logged in user
        /// </summary>
        public UserViewModel LoggedInUser
        {
            get
            {
                if (_currentUser == null)
                    return SessionCache.CurrentUser;
                else
                    return _currentUser;
            }            
        }

        public ActionResult RedirectToAccessDenied()
        {
            //return RedirectToAction("Index", "PermissionDenied");
            return RedirectToRoute(AppConstants.RouteNames.Public.DEFAULT, new { controller = "Account", action = "PermissionDenied" });
        }
        #region Testing Methods
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
        }
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
        #endregion 
    }
}